package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.OceanengineCreateGroupResult;
import com.ylyq.ylxx.model.bean.OceanengineGroupInfo;
import com.ylyq.ylxx.model.bean.OceanengineReportGroupInfo;
import com.ylyq.ylxx.model.bean.OceanengineUpdateStatusGroupResult;
import com.ylyq.ylxx.model.request.*;
import com.ylyq.ylxx.service.IOceanegineGroupService;
import com.ylyq.ylxx.service.IOceanegineReportService;
import com.ylyq.ylxx.util.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IOceanegineGroupServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/6/24 17:10
 * @Version 1.0
 */
@Service
public class OceanegineGroupServiceImpl implements IOceanegineGroupService {

    @Autowired
    private IOceanegineReportService iOceanegineReportService;

    @Override
    public OceanengineGroupInfo getGroup(GetGroupRequest getGroupInfoRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "campaign/get/";

        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getGroupInfoRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineGroupInfo oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineGroupInfo.class);
        return oceanengineAccount;
    }

    @Override
    public ResponseData getGroupAndReport(GetGroupInfoRequest getGroupInfoRequest) {
        try {
            GetGroupRequest getGroupRequest=new  GetGroupRequest();
            BeanUtils.copyProperties(getGroupInfoRequest,getGroupRequest);
            OceanengineGroupInfo oceanengineAccount = getGroup(getGroupRequest);
            List<OceanengineGroupInfo.OceanengineGroupDataBean.OceanengineGroupListBean> list = oceanengineAccount.getData().getList();
            if (null != list)
                for (OceanengineGroupInfo.OceanengineGroupDataBean.OceanengineGroupListBean temp : list) {

                    GetReportGroupRequest getReportGroupRequest = new GetReportGroupRequest();
                    getReportGroupRequest.setAdvertiser_id(getGroupInfoRequest.getAdvertiser_id());
                    getReportGroupRequest.setStart_date(getGroupInfoRequest.getReportFiltering().getStart_date());
                    getReportGroupRequest.setEnd_date(getGroupInfoRequest.getReportFiltering().getEnd_date());

                    GetReportGroupRequest.FilteringGetReportGroupRequest filtering = new GetReportGroupRequest.FilteringGetReportGroupRequest();
                    filtering.setCampaign_ids(new Long[]{temp.getId()});
                    getReportGroupRequest.setFiltering(filtering);
                    getReportGroupRequest.setGroup_by(new String[]{"STAT_GROUP_BY_FIELD_ID"});

                    OceanengineReportGroupInfo reportGroup = iOceanegineReportService.getReportGroup(getReportGroupRequest);

                    if (null != reportGroup && null != reportGroup.getData().getList() && !reportGroup.getData().getList().isEmpty())
                        temp.setReportdetail(reportGroup.getData().getList().get(0));
                }

//            oceanengineAccount.getData().setList(list);
            return new ResponseData(ResponseData.Result.SUCCESS, oceanengineAccount);
        } catch (NullPointerException e) {
            e.printStackTrace();
            return new ResponseData(ResponseData.Result.NODATA);
        }

    }

    @Override
    public OceanengineCreateGroupResult createGroup(CreateGroupInfoRequest createGroupInfoRequest) {

        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "campaign/create/";

        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSON(createGroupInfoRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineCreateGroupResult oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineCreateGroupResult.class);
        return oceanengineAccount;

    }

    @Override
    public OceanengineCreateGroupResult updateGroup(UpdateGroupInfoRequest updateGroupInfoRequest) {

        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "campaign/update/";

        // 请求参数
        Map<String, Object> data = new HashMap();
//        {
//            {
//                put("advertiser_id", updateGroupInfoRequest.getAdvertiser_id());
//                put("campaign_name", updateGroupInfoRequest.getCampaign_name());
//                put("budget", updateGroupInfoRequest.getBudget());
//                put("campaign_id", updateGroupInfoRequest.getCampaign_id());
//                put("modify_time", updateGroupInfoRequest.getModify_time());
//            }
//        };
        if (updateGroupInfoRequest.getAdvertiser_id() != null) {
            data.put("advertiser_id", updateGroupInfoRequest.getAdvertiser_id());
        }
        if (StringUtils.isNotBlank(updateGroupInfoRequest.getCampaign_name())) {
            data.put("campaign_name", updateGroupInfoRequest.getCampaign_name());
        }
        if (StringUtils.isNotBlank(updateGroupInfoRequest.getBudget_mode())) {
            data.put("budget_mode", updateGroupInfoRequest.getBudget_mode());
        }
        if (updateGroupInfoRequest.getBudget() != null) {
            data.put("budget", updateGroupInfoRequest.getBudget());
        }
        if (updateGroupInfoRequest.getCampaign_id() != null) {
            data.put("campaign_id", updateGroupInfoRequest.getCampaign_id());
        }
        if (StringUtils.isNotBlank(updateGroupInfoRequest.getModify_time())) {
            data.put("modify_time", updateGroupInfoRequest.getModify_time());
        }


        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSONString(data), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineCreateGroupResult oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineCreateGroupResult.class);
        return oceanengineAccount;
    }

    @Override
    public OceanengineUpdateStatusGroupResult updateStatusGroup(UpdateStatusGroupInfoRequest updateStatusGroupInfoRequest) {

        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "campaign/update/status/";

        // 请求参数
        Map<String, Object> data = new HashMap();
//        {
//        {
//            put("advertiser_id", updateStatusGroupInfoRequest.getAdvertiser_id());
//            put("campaign_ids", updateStatusGroupInfoRequest.getCampaign_ids());
//            put("opt_status", updateStatusGroupInfoRequest.getOpt_status());
//        }
//    };
        if (updateStatusGroupInfoRequest.getAdvertiser_id() != null) {
            data.put("advertiser_id", updateStatusGroupInfoRequest.getAdvertiser_id());
        }
        if (updateStatusGroupInfoRequest.getCampaign_ids() != null && updateStatusGroupInfoRequest.getCampaign_ids().length > 0) {
            data.put("campaign_ids", updateStatusGroupInfoRequest.getCampaign_ids());
        }
        if (StringUtils.isNotBlank(updateStatusGroupInfoRequest.getOpt_status())) {
            data.put("opt_status", updateStatusGroupInfoRequest.getOpt_status());
        }

        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSONString(data), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineUpdateStatusGroupResult oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineUpdateStatusGroupResult.class);
        return oceanengineAccount;
    }


}
